<?php
	//Database
	require_once $_SERVER['DOCUMENT_ROOT'] . '/deviantpic/inc/database.php';
	
	//Get user
	$id		= 0;
	$action = 0;
	if (isset($_GET['id']))		$id = $_GET['id'];
	if (isset($_GET['action']))	$action = $_GET['action'];

	//Get user
	$user	= NULL;
	$result = mysql_query("SELECT * FROM users WHERE id=$id") or die(mysql_error());

	//Esnsure there's a user
	if (mysql_num_rows($result) < 1) header("Location:http://" . $_SERVER['HTTP_HOST'] . '/deviantpic/index.php');
	else {
		//Get user data
		$user		= mysql_fetch_array($result);
		$following	= FALSE;

		//If logged in
		if (isset($_SESSION['user']) && is_numeric($_SESSION['user'])) {
			//If not user
			if ($user['id'] != $_SESSION['user']) {

				//Check database
				$result = mysql_query("SELECT * FROM followers WHERE user_id=". $user['id'] . " AND follower_id=" . $_SESSION['user']) or die(mysql_error());
				if (mysql_num_rows($result) >= 1) $following = TRUE;

				//If follow request
				if ($action == 1 && !$following) {
					//Create query
					mysql_query("INSERT INTO followers (user_id, follower_id) VALUES(" . $user['id'] . "," . $_SESSION['user'] . ")");
					$following = TRUE;
				} else if ($action == 2 && $following) {
					//Create query
					mysql_query("DELETE FROM followers WHERE user_id='" . $user['id'] . "' AND follower_id='" . $_SESSION['user'] . "'");
					$following = false;
				}
			}
		}
	}

	function create_anchor($page, $action) {
		//Get user
		global $user;
		
		//Href opening
		$anchor = "href='";

		//Numerize action
		$action_index = -1;
		if ($action == "follow")		$action_index = 1;
		else if ($action == "unfollow") $action_index = 2;
		else if ($action == "delete")	$action_index = 3;

		//URL
		$anchor .= $page . ".php";
		$anchor .= "?id=" . $user['id'];
		if ($action_index != -1)
			$anchor .="&action=" . $action_index;

		//Ending
		$anchor .= "'";

		//Return
		return $anchor;
	}

	function write_username($login_user, $current) {
		//Get user
		global $user, $following;

		//is following?
		if ($following) $text = "unfollow";
		else			$text = "follow";

		//Write first part
		echo "
			<div id='username'>
				<span class='username'>" . $user['username'] . "</span>
				<span class='follow'>";

		//If not logged in user
		if (isset($login_user) && $user['id'] != $login_user)
			echo "<a class='follow' " . create_anchor($current, $text) . ">" . $text . "</a>";

		//End
		echo "
				</span>
			</div>";
	}

	function write_usertabs($login_user, $current) {
		//Get user
		global $user;

		//Create array
		$tabs	= array("Profile", "Wall", "Pictures", "Inbox", "Administrator");
		$pages	= array("profile", "wall", "gallery", "inbox", "admin");

		//Check privilege
		$wall = true;
		if ($user['wall_privilege'] == 3) {
			if (!isset($_SESSION['user']))				$wall = false;
			else if ($user['id'] != $_SESSION['user'])	$wall = false;
		}
		if ($user['wall_privilege'] == 2) {
			if (!isset($login_user) && $user['wall_privilege'] == 2) $wall = false;
			else {
				//Find
				$result = mysql_query("SELECT * FROM followers WHERE user_id='" . $user['id'] . "' AND follower_id='" . $_SESSION['user'] . "'") or die(mysql_error());
				if (mysql_num_rows($result) < 1) $wall = false;
			}
		}
		
		//Opening tags
		echo '
			<div id="tabs">
				<ul class="tabs">';

		//For each tab
		for ($i = 0; $i < count($tabs); $i++) {
			//If not inbox or user
			if ($tabs[$i] != "Inbox" || $user['id'] == $login_user) {
				if (($tabs[$i] != "Wall" || $wall) && ($tabs[$i] != "Administrator" || ($user['role'] == 1 && $user['id'] == $login_user))) {
					//Get class
					$class = "other-tab";
					if ($current == $i) $class = "current-tab";

					//Write
					echo "<li class='tab'><span class='$class'>";
					echo "<a " . create_anchor($pages[$i], null) . " ";
					echo 'onClick="return changeTab('. "'" . $pages[$i] . "'" .',' . $user['id'] . ');" >';
					echo $tabs[$i];
					echo "</a>";
					echo "</span></li>";
				}
			}
		}
		//Close tags
		echo '
					</ul>
			</div>';
	}

?>
